--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 机器人逻辑

local skynet = require "skynet"
local log = require "Log"
local Network = require "Network"
local roomConfig = require "lua_BjlTableList"
local NodeMessage = require "NodeMessage"
require "setting"
local Logic = class("Logic")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function Logic:ctor(data)
	--随机种子
    local time = tostring(os.time())
    math.randomseed(time:reverse():sub(1, 7))
	local svr_config = robot_setting['baijiale']
	self.playerid = data.mid
	self.player = nil
	-- self.user = user
	self.ip = svr_config.ip
	self.port = svr_config.port		
	self.tbConf = {} --场次配置
	self.robotType = data.type
	for _,v in pairs(roomConfig) do 
		table.insert(self.tbConf,v)
	end
	table.sort(self.tbConf,function (a,b)
		if a.min_coin < b.min_coin then 
			return true
		end
	end)
	self.roomConf = self.tbConf[self.robotType] --要入的场
	self.onlineTime = 0
	self.ExitTime = math.random(10,30) * 60
	self.playingTime = 0
	self.leaveTableTime = math.random(3,9) * 60


	self.nodeMessage = NodeMessage.new()
	--开启网络模块
	self.network = Network.new(self.ip,self.port)
	--注册消息回调
	self:register()

	self:login(self.playerid)
	self:closeByTime()
end

function Logic:register()
	self.network:register('php_res',handler(self,self.php_res))
	self.network:register('logout_res',handler(self,self.logout_res))
	self.network:register('room_state_res',handler(self,self.room_state_res))
	self.network:register('enter_table_res',handler(self,self.enter_table_res))
	self.network:register('leave_table_res',handler(self,self.leave_table_res))	
	self.network:register('changetable_res',handler(self,self.changetable_res))		
	self.network:register('reconnect_res',handler(self,self.reconnect_res))
	self.network:register('nt_broadcast',handler(self,self.nt_broadcast))
	self.network:register('nt_goods_change',handler(self,self.nt_goods_change))
	self.network:register('nt_enter_gate',handler(self,self.nt_enter_gate))
	self.network:register('nt_kick_table',handler(self,self.nt_kick_table))

	
	self.network:register('exit',handler(self,self.exit))

	self.network:register('bjl_nt_seat_change',handler(self,self.bjl_nt_seat_change))	
	self.network:register('bjl_nt_rest',handler(self,self.bjl_nt_rest))
	self.network:register('bjl_nt_start',handler(self,self.bjl_nt_start))
	self.network:register('bjl_nt_end',handler(self,self.bjl_nt_end))
	self.network:register('bjl_nt_throw',handler(self,self.bjl_nt_throw))
	self.network:register('bjl_nt_throw_tail',handler(self,self.bjl_nt_throw_tail))
	self.network:register('bjl_nt_other_bet',handler(self,self.bjl_nt_other_bet))
	self.network:register('bjl_nt_card',handler(self,self.bjl_nt_card))


	self.network:register('bjl_status_res',handler(self,self.bjl_status_res))
	self.network:register('bjl_bet_res',handler(self,self.bjl_bet_res))
	self.network:register('bjl_remain_card_res',handler(self,self.bjl_remain_card_res))
	self.network:register('bjl_rebet_res',handler(self,self.bjl_rebet_res))
		

end

function Logic:exit()
	skynet.sleep(100)
	self.network:close()
	skynet.exit()
end

function Logic:showError(data)
	if data.err_no and data.err_no > 0 then 
		print("_____error__",data)
		self:exit()
		return false
	end
	return true
end


--账号
function Logic:login(mid)
	local str = '{"ver":"1.0.0","appid":10100100,"action":"user.guestLogin","platform":1,"cmd":10000}'
	if mid then 
		str = '{"ver":"1.0.0","appid":10100100,"mid":'..mid..',"action":"user.guestLogin","platform":1,"cmd":10001}'
	end

	local data = {php_req={
 		id = 10000,
 		content = str,
	}}
	--print(data)
	self.network:send(data)
end


local num = 0
function Logic:php_res(data)
	-- print("___php_res",data)
	if not self:showError(data) then 
		return 
	end


	-- num = num + 1
	-- if num > 1 then 
	-- 	return
	-- end
	-- self:logout_req()
end

function Logic:nt_enter_gate(data)
	--进入大厅成功
	local delay = math.random(100,300)
	skynet.sleep(delay)
	--进入场次
	self:enterTable(self.roomConf.id)
end

--登出
function Logic:logout_req()
	local data = {logout_req={playerid=1}}
	self.network:send(data)
end
function Logic:logout_res(data)
	if not self:showError(data) then 
		return 
	end
	self:login(self.playerid)
end

--场次信息
function Logic:room_state_req(gameid)
	local data = {room_state_req={
 		gameid=gameid, 		
	}}
	print(data)
	self.network:send(data)
end
function Logic:room_state_res(data)

end

function Logic:enterTable(room_type)
	local data = {enter_table_req={
 		room_type=room_type, 		
	}}
	self.network:send(data)		
end
function Logic:enter_table_res(data)
	-- if not self:showError(data) then 
	-- 	return 
	-- end
	print("____enter_table____",data)
	if data.err_no and data.err_no > 0 then 
		if data.err_no == 12 then 
			print("____________金币不足___",self.playerid,self.robotType)
			data = {playerid=self.playerid,robotType=self.robotType}
			self.nodeMessage:sendService('manager','change_money',data)
			self:exit()
			return 
		elseif data.err_no then 
			self:exit() 
			return
		end
	end
	if data.tableid > 0 then 
		self.tableid = data.tableid
		self:bjlStatusReq()
	end
	--self:reconnect_req()
	
end

function Logic:reconnect_req()
	local data = {reconnect_req={
 		tableid=self.tableid, 		
	}}
	self.network:send(data)
end
function Logic:reconnect_res(data)
	print("#####reconnect_res#####",data)
end

--请求桌子状态
function Logic:bjlStatusReq()
	local data = {bjl_status_req={
		tableid = self.tableid,
	}}
	self.network:send(data)
end
function Logic:bjl_status_res(data)
	if not self:showError(data) then 
		return 
	end
	self.player = data.seat_player
	self.config = data.config
	self.chipList = data.config.chip_list
end

--休息
function Logic:bjl_nt_rest(data)

	if self.onlineTime > self.ExitTime then 
		self:exit()
		return
	end			
	-- if self.onlineTime > self.leaveTableTime then 
	-- 	-- print("___onlineTime________________",self.onlineTime , self.leaveTableTime)
	-- 	self.leaveTableTime = self.leaveTableTime + self.onlineTime
	-- 	skynet.sleep(math.random(100,200))
	-- 	local randNum = math.random(1,3)		
	-- 	if randNum == 2 then 
	-- 		self:leaveTableReq()
	-- 		return 
	-- 	else		
	-- 	end				
	-- end

end

--离开桌子
function Logic:leaveTableReq()
	local data = {
	leave_table_req = {
			tableid = self.tableid,
		}
	}
	self.network:send(data)
end
function Logic:leave_table_res(data)
	-- print("__________ leave_table_res",data)
	if not self:showError(data) then 
		return 
	end
	skynet.sleep(math.random(1000,2000))
	--进入场次
	self:enterTable(self.roomConf.id)
end

--开始游戏
function Logic:bjl_nt_start(data)
	print("___bjl_nt_start___",data)
	local data = {
		bjl_bet_req={
			tableid = self.tableid,
			add_chip = self.chipList[1],
			poolid = 1,
		}
	}
	self.network:send(data)		

	skynet.sleep(100)
	-- local data = {
	-- 	bjl_rebet_req={
	-- 		tableid = self.tableid,
	-- 	}
	-- }
	-- self.network:send(data)	
	
	-- local data = {
	-- 	bjl_remain_card_req={
	-- 		tableid = self.tableid,
	-- 	}
	-- }
	-- self.network:send(data)	
end

--结束
function Logic:bjl_nt_end(data)
	print("____bjl_nt_end__",data)
end

--去牌头
function Logic:bjl_nt_throw(data)
	print("___bjl_nt_throw___",data)
end

function Logic:bjl_nt_throw_tail(data)
	print("___bjl_nt_throw_tail___",data)
end

function Logic:bjl_nt_other_bet(data)
	print("___bjl_nt_other_bet___",data)
end

function Logic:bjl_nt_card(data)
	print("___bjl_nt_card___",data)
end


--换桌
function Logic:changetable_req()
	local data = {
		changetable_req={tableid = self.tableid}
	}
	self.network:send(data)
end
function Logic:changetable_res(data)

end

--座位发生改变
function Logic:bjl_nt_seat_change()
	
end

--个人属性改变
function Logic:nt_goods_change()
	
end


function Logic:bjl_bet_res(data)
	print("___bjl_bet_res___",data)
end

function Logic:bjl_rebet_res(data)
	print("___bjl_rebet_res___",data)
end

function Logic:bjl_remain_card_res(data)
	print("___bjl_remain_card_res___",data)
end


function Logic:nt_broadcast(data)
end

function Logic:nt_kick_table(data)
	--被踢出房间
	skynet.exit()
end

function Logic:closeByTime()
	--3 - 10分钟后退出游戏
	skynet.fork(function ()
		while (true) do 
			skynet.sleep(100)
			self.onlineTime = self.onlineTime + 1
			
		end
	end)
end


return Logic